package org.example.dishCategory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.dishCategory.service.IDishCategoryService;
import org.example.dishCategory.vo.QueryCategory;
import org.example.dish_pojo.DishCategory;
import org.example.dish_pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 * 菜品类别表（支持多级分类） 前端控制器
 * </p>
 *
 * @author mp
 * @since 2025-07-30
 */
@RestController
@RequestMapping("/dish-category")
public class DishCategoryController {

    @Autowired
    private IDishCategoryService dishCategoryService;


    // 添加菜品类别
    @RequestMapping("/addDishCategory")
    public Result addDishCategory(@RequestBody DishCategory dishCategory) {
        boolean save = dishCategoryService.save(dishCategory);
        if (!save) {
            return new Result(500, "添加失败", null);
        }
        return new Result(200, "添加成功", null);
    }


    // 删除菜品类别
    @RequestMapping("/deleteDishCategory")
    public Result deleteDishCategory(Integer categoryId) {
        boolean delete = dishCategoryService.removeById(categoryId);
        if (!delete) {
            return new Result(500, "删除失败", null);
        }
        return new Result(200, "删除成功", null);
    }


    // 批量删除菜品类别
    @RequestMapping(value = "/deleteDishCategorys", method = RequestMethod.POST)
    public Result deleteDishCategorys(@RequestParam("categoryIds") Integer[] categoryIds) {
        boolean delete = dishCategoryService.removeByIds(Arrays.asList(categoryIds));
        if (!delete) {
            return new Result(500, "删除失败", null);
        }
        return new Result(200, "删除成功", null);
    }


    // 根据id回显数据
    @RequestMapping("/getCategoryById")
    public Result getCategoryById(@RequestParam("categoryId") Integer categoryId) {
        DishCategory category = dishCategoryService.getById(categoryId);
        return new Result(200, "操作成功", category);
    }


    // 修改菜品类别
    @RequestMapping("/updateDishCategory")
    public Result updateDishCategory(@RequestBody DishCategory dishCategory) {
        boolean update = dishCategoryService.updateById(dishCategory);
        if (!update) {
            return new Result(500, "修改失败", null);
        }
        return new Result(200, "修改成功", null);
    }


    // 分页查询菜品类别
    @RequestMapping("/listCategory")
    public Result listCategory(@RequestBody QueryCategory queryCategory) {
        Page<DishCategory> page = new Page<>(queryCategory.getCurrent(), queryCategory.getSize());
        QueryWrapper<DishCategory> wrapper = new QueryWrapper<>();
        wrapper.like(queryCategory.getCategoryName() != null, "category_name", queryCategory.getCategoryName());
        Page<DishCategory> pageData = dishCategoryService.page(page, wrapper);
        return new Result(200, "操作成功", pageData);
    }


}

